<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH."libraries/AdminController.php");
class Formulir extends AdminController {  
	
	var $status_form = "'ENTRY','UPLOAD'";
	var $dis = "";
	var $is_detail = FALSE;
	
	function __construct()   
	{
		parent::__construct();    
		$this->_set_action();
		$this->_set_action(array("detail"),"ITEM");
		$this->cat_search = array(
			'all'						=> 'Pilih Kategory',
			'form_principal_name'		=> 'Nama Principal',
			'form_obligee_name'			=> 'Nama Obligee',
			'form_bank_garansi_type'	=> 'Tipe Bank Garansi'
			/*'form_obligee_address'		=> 'Alamat Obligee',			
			'form_principal_address'	=> 'Alamat Principal',
			'form_document_number'		=> 'Nomor Dokumen',
			'form_document_desc'		=> 'Penjelasan Dokumen',
			'marketing_name'			=> 'Nama Marketing',
			'cur_name'					=> 'Nama Mata Uang'*/
		);
		$this->_set_title('Transaksi > Formulir');
		$this->_set_cat_search(array("all"	=> $this->cat_search));	
		$this->DATA->table="tm_formulir";
		$this->load->model("mdl_formulir","A");
		$this->folder_view = "transaksi/";
		$this->upload_path="./uploaded/formulir/";
		$this->prefix_view = strtolower($this->_getClass());
		
		//$this->output->enable_profiler(true);

	}

	function index(){
		
		$this->jCfg['isearch']['status'] = "all";
		$this->jCfg['isearch']['per_page'] = 5;
		$this->_releaseSession();
		
		$this->_set_desc_title('Daftar Formulir');	
		
		$mst=isset($this->jCfg['isearch']['status'])?$this->jCfg['isearch']['status']:"all";
		
		$stat = ($mst=="all")?$this->status_form:"'".$mst."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
				"limit" 	=> $this->per_page, 
				"status"	=> $stat,
				"start_date"=> $this->jCfg['isearch']['start_date'],
				"end_date"	=> $this->jCfg['isearch']['end_date'],
				"offset" 	=> $this->uri->segment($this->uri_segment)
		) );
		
		$data = $this->_data(array(
				'base_url'		=> $this->own_link.'index'
			)
		);	
					
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function search(){
		$this->_set_desc_title('Daftar Formulir');	
		$this->load->helper('url');		
		$this->uri_segment = 6;
			
		$column = ''; $keyword = '';				
		if( isset($_POST['column']) && isset($_POST['keyword']) ){
			$column = $_POST['column'];
			$keyword = url_title($_POST['keyword']);
			$this->setiSearch(array(
				"start_date"=> dbClean($_POST['dstart']),
				"end_date"	=> dbClean($_POST['dend']),
				"status"	=> dbClean($_POST['status']),
				"per_page"	=> dbClean($_POST['per_page'])
			));
		}else{
			$column =  $this->uri->segment(4);
			$keyword =   $this->uri->segment(5);
		}	
		
		$stat = ($this->jCfg['isearch']['status']=="all")?$this->status_form:"'".$this->jCfg['isearch']['status']."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
								"limit" 	=> $this->per_page, 
								"offset" 	=> $this->uri->segment($this->uri_segment),
								"column"	=> $column,
								"start_date"=> $this->jCfg['isearch']['start_date'],
								"end_date"	=> $this->jCfg['isearch']['end_date'],
								"status"	=> $stat,
								"keyword"	=> (trim($keyword)=="all")?"":$keyword
							) );
		$k=($keyword=="")?"all":$keyword;
		$data = $this->_data(
			array(
				'base_url'		=>$this->own_link.'search/'.$column.'/'.$k
			)
		);	
		
		
		$this->header_page['key']=pTxt(($k=="all")?"":$k);
		$this->header_page['col']=$column;	
		$this->header_page['cat_search'] = array($column	=>	$this->cat_search);	
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function add(){			
		$this->_set_desc_title('Tambah Forulir');		
		$this->DATA->table = "tm_pialang_marketing";
		$marketing = $this->DATA->_getall();
		
		$this->DATA->table = "tm_currency";
		$curr = $this->DATA->_getall();
		
		$this->_v($this->folder_view.$this->prefix_view."_form",array(
			"marketing"	=> $marketing,
			"curr"		=> $curr
		));
	}
	
	function edit($id=''){
		$id=dbClean(trim($id));
		$this->_set_desc_title('Update Data Formulir');	
		
		if(trim($id)!=''){
			$this->data_form = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
				
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}
	
	function detail($id=''){
		$this->is_detail = TRUE;
		$this->dis = 'disabled="disabled"';
		
		$id=dbClean(trim($id));
		
		if(trim($id)!=''){
			$this->data_form = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
				
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}
	
	function remove($key = ""){
		if($key=="")die("Access Denied");
		
		$this->DATA->table = "tm_file";
		$file = $this->DATA->data_id(array(
			"file_secret_key"	=> $key
		));
		if(count($file)==0) die("Access Denied");
		
		$fpath = $this->upload_path.$file->file_location.$file->file_name;
		if(file_exists($fpath) && !is_dir($fpath)){
			@unlink($fpath);	
		}
		
		$this->DATA->_delete(array(
			"file_secret_key"	=> $key
		));
		
		if(isset($_GET['url']))
			redirect($_GET['url']);
		else
			redirect($this->own_link);
	}
	
	
	function delete($id=''){
		$id=dbClean(trim($id));		
		if(trim($id) != ''){
			$p = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			if($p->form_status != 'ENTRY'){
				die('Access Denied');
			}
			
			$o = $this->DATA->_delete(
				array("form_id"	=> idClean($id))
			);
			
			$this->message = ($o)?_l('success_exec'):_l('fail_exec');
			
		}
		redirect($this->own_link);
	}

	function save(){	
		$data = array(
			'form_principal_name' 		=> dbClean($_POST['form_principal_name']),
			'form_principal_address' 	=> dbClean($_POST['form_principal_address']),
			'form_obligee_name' 		=> dbClean($_POST['form_obligee_name']),
			'form_obligee_address' 		=> dbClean($_POST['form_obligee_address']),
			'form_marketing' 			=> dbClean($_POST['form_marketing']),
			'form_bank_garansi_type' 	=> dbClean($_POST['form_bank_garansi_type']),
			'form_mata_uang' 			=> dbClean($_POST['form_mata_uang']),
			'form_contract_value' 		=> dbClean($_POST['form_contract_value']),
			'form_guarantee_value' 		=> dbClean($_POST['form_guarantee_value']),
			'form_document_number' 		=> dbClean($_POST['form_document_number']),
			'form_document_desc' 		=> dbClean($_POST['form_document_desc']),
			'form_job' 					=> dbClean($_POST['form_job']),
			'form_status' 				=> 'ENTRY',
			'form_as_long_day' 			=> dbClean($_POST['form_as_long_day']),
			'form_start_date' 			=> dbClean($_POST['form_start_date']),
			'form_end_date' 			=> dbClean($_POST['form_end_date'])
		);

		$a = $this->_save_master( 
			$data,
			array(
				'form_id' => dbClean($_POST['form_id'])
			),
			dbClean($_POST['form_id'])			
		);
		
		foreach($_FILES as $k => $v){
			$this->upload_formulir(
				array(
					'id'		=> $a['id'],
					'input'		=> $k
				)
			);
		}
		
		redirect($this->own_link);
	}
	
	function upload_formulir($par=array()){
		if($_FILES[$par['input']]['error']==4)
			return false;
		
		$uId = rand(1,100000)."-".uniqid();
		$uri = $this->upload_path;
		$location = $uri."".date("Y-m-d");
		$nlocation = $location."/".$par['id'];
		
		if(!is_dir($location)){
			@mkdir($location);
		}
		
		if(!is_dir($nlocation)){
			@mkdir($nlocation);
		}
		
		$fileName = $uId;
		$config['upload_path'] 		= $nlocation."/";
		$config['file_name'] 		= $fileName;
		$config['allowed_types'] 	= 'doc|docx|xls|xlsx|pdf|zip|rar|gif|jpg|png';
		$config['max_size']			= 1024*10;
		$this->load->library('upload');
		
		$this->upload->initialize($config);
	
		if( $this->upload->do_upload($par['input']) )
		{	
			$file = $uId.$this->upload->file_ext;		
			$this->DATA->table = "tm_file";	
			$this->DATA->_add(array(
				'file_form_id'		=> $par['id'],
				'file_location'		=> date("Y-m-d")."/".$par['id']."/",
				'file_secret_key'	=> $fileName.md5("meme"),
				'file_name'			=> $file,
				'file_owner'		=> $this->jCfg['user']['id'],
				'file_status'		=> 0,
				'file_date'			=> date("Y-m-d H:i:s")					
			));
		}
	}

}
